package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import com.yanqu.road.web.controller.chargecenter.RechargeConditionResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet(urlPatterns = "/H5ServerList")
public class H5ServerList extends BaseJsonHttpServlet {

    private static Logger logger = LogManager.getLogger(H5ServerList.class.getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("request H5ServerList");
        List<ServerInfo> openServerList = ServerListMgr.getOpenServerList();
        List<H5ServerData> list = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (ServerInfo info : openServerList) {
            if (info.getChannelId() != 301 && info.getChannelId() != 321 && info.getChannelId() != 311 && info.getChannelId() != 331) {
                continue;
            }
            H5ServerData data = new H5ServerData();
            data.setDsid(info.getServerId() + "");
            data.setDsname(info.getServerName());
            data.setStart_time(format.format(new Date(info.getOpenTime() * 1000)));
            int statue = 0;
            if (info.isOpen()) {
                statue = 2;
            }
            data.setStatue(statue);
            list.add(data);
        }
        RechargeConditionResult conditionResult = new RechargeConditionResult();
        conditionResult.setState(1);
        conditionResult.setData(list);
        String result = JSON.toJSONString(conditionResult);
        sendMessage(response, result);
    }


    class H5ServerData {
        private String dsid;
        private String dsname;
        private String start_time;
        private int statue;

        public String getDsid() {
            return dsid;
        }

        public void setDsid(String dsid) {
            this.dsid = dsid;
        }

        public String getDsname() {
            return dsname;
        }

        public void setDsname(String dsname) {
            this.dsname = dsname;
        }

        public String getStart_time() {
            return start_time;
        }

        public void setStart_time(String start_time) {
            this.start_time = start_time;
        }

        public int getStatue() {
            return statue;
        }

        public void setStatue(int statue) {
            this.statue = statue;
        }
    }
}
